package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.PageView;
import cn.wolfcode.trip.base.mapper.PageViewMapper;
import cn.wolfcode.trip.base.service.IPageViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PageViewServiceImpl implements IPageViewService {
    @Autowired
    private PageViewMapper pageViewMapper;
    /**
     * 游记浏览量计算
     * @param travelId
     * @return
     */
    @Override
    public Long travelview(Long travelId) {
        //判断数据库内是否有此travel_id
        if (pageViewMapper.selectByTravelId(travelId) == null){
            PageView pageView = new PageView();
            pageView.setTravelId(travelId);
            pageViewMapper.insert(pageView);
        }
        PageView pageView = pageViewMapper.selectByTravelId(travelId);
        Long travelview;
        if (pageView.getTravelview() != null){
            travelview = pageView.getTravelview();
            travelview = travelview+1;
            pageView.setTravelview(travelview);
            pageViewMapper.updateByPrimaryKey(pageView);
            return travelview;
        }else {
            pageView.setTravelview(1L);
            pageViewMapper.updateByPrimaryKey(pageView);
            return 1L;
        }
    }
}
